package com.nnnu.orange.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.nnnu.orange.constant.MessageConstants;
import com.nnnu.orange.pojo.dto.BorrowingDto;
import com.nnnu.orange.pojo.dto.BorrowingQueryDto2;
import com.nnnu.orange.pojo.dto.ReturnBookDto;
import com.nnnu.orange.pojo.entity.Borrowings;
import com.nnnu.orange.result.Result;
import com.nnnu.orange.service.IBorrowingsService;
import com.nnnu.orange.utils.ThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 记录用户借阅图书的信息 前端控制器
 * </p>
 *
 * @author zk
 * @since 2024-11-07
 */
@RestController("userBorrowingsController")
@RequestMapping("/user/borrowings")
@Slf4j
@Api("用户端借阅图书相关接口")
@RequiredArgsConstructor
public class BorrowingsController {

    private final IBorrowingsService borrowingsService;


    /**
     * 查询借阅记录数据
     *
     * @param userId 用户ID
     * @return 用户下的借阅列表
     */
    @GetMapping("/list/{userId}")
    @ApiOperation("分页查询借阅记录数据")
    public Result<?> list(@PathVariable Integer userId) {
        log.info("用户查询借阅记录列表:{}", userId);
        return Result.success(borrowingsService.list(new LambdaQueryWrapper<Borrowings>()
                .eq(Borrowings::getUserId, ThreadLocalUtil.getId())), MessageConstants.SELECT_OK);
    }


    /**
     * 管理员获取借阅详细信息
     *
     * @param borrowingId 借阅唯一id
     * @return 借阅实体
     */
    @GetMapping("/{borrowingId}")
    @ApiOperation("用户获取借阅详细信息")
    public Result<?> detail(@PathVariable Integer borrowingId) {
        log.info("用户获取借阅详细信息:{}", borrowingId);
        return Result.success(borrowingsService.findById(borrowingId), MessageConstants.SELECT_OK);
    }

    /**
     * 用户借阅图书
     *
     * @param borrowingDto dto
     * @return 提示
     */
    @PostMapping("/borrowing")
    @ApiOperation("用户借阅图书")
    public Result<?> userBorrowing(@RequestBody BorrowingDto borrowingDto) {
        log.info("用户借阅图书:{}", borrowingDto);
        borrowingsService.userBorrowing(borrowingDto);
        return Result.success(MessageConstants.BORROWING_OK);
    }


    /**
     * 用户归还书籍
     *
     * @param returnBookDto dto
     * @return 提示
     */
    @PostMapping("/return")
    @ApiOperation("用户归还书籍")
    public Result<?> returnBook(@RequestBody ReturnBookDto returnBookDto) {
        log.info("用户归还书籍：{}", returnBookDto);
        borrowingsService.returnBook(returnBookDto);
        return Result.success(MessageConstants.RETURN_OK);
    }
}
